package cn.com.huzo.service.master.impl;

import cn.com.huzo.common.util.ApplicationConfiguration;
import cn.com.huzo.model.entities.master.EndUserEntity;
import huzofw.model.entities.paginator.Paginator;
import cn.com.huzo.repository.master.EndUserRepository;
import huzofw.service.impl.GenericManagerImpl;
import cn.com.huzo.service.master.EndUserManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 终端客户信息管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("EndUserManager")
public class EndUserManagerImpl extends GenericManagerImpl<EndUserEntity, Long>
        implements EndUserManager {

    private final String rootNodeText = (String) ApplicationConfiguration.getProperty("Catalogue_EndUser");

    @Resource(name = "EndUserRepository")
    private EndUserRepository _dao;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public EndUserEntity fetchByKey(Long key) throws Exception {
        return _dao.fetchByKey(rootNodeText, key);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> criterion) throws Exception {
        Map<String, Object> rtnValue = new HashMap();

        Long offset = (long) (MapUtils.getInteger(criterion, "Limit") * (MapUtils.getInteger(criterion, "PageIndex") - 1));
        long total = _dao.fetchForCount(criterion);

        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<EndUserEntity> itemList = _dao.fetchForPage(criterion);
            Paginator<EndUserEntity> paginator = new Paginator<>(itemList, offset, MapUtils.getInteger(criterion, "Limit"), total);

            rtnValue.put("TotalRecordCount", paginator.getTotal());
            rtnValue.put("TotalPages", paginator.getTotalPages());
            rtnValue.put("PageIndex", paginator.getPageIndex());
            rtnValue.put("DataList", paginator.getItemList());
            rtnValue.put("Success", true);
        } else {
            rtnValue.put("Success", false);
        }

        return rtnValue;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Long insert(EndUserEntity instance) throws Exception {
        Long key = _dao.insert(instance);
        // 审计日志
        if (key > 0) {
            writeAuditLog(instance.getUpdatedBy(), "基础信息→终端客户信息", "终端客户编号：" + instance.getEndUserNo(), "增加");
        }

        return key;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer update(EndUserEntity instance) throws Exception {
        Integer rowsAffected = _dao.update(instance);
        // 审计日志
        if (rowsAffected > 0) {
            writeAuditLog(instance.getUpdatedBy(), "基础信息→终端客户信息", "终端客户编号：" + instance.getEndUserNo(), "更新");
        }

        return rowsAffected;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        // 删除
        Integer rowsAffected = _dao.delete(MapUtils.getLong(criterion, "EndUserId"));
        // 审计日志
        if (rowsAffected > 0) {
            writeAuditLog(MapUtils.getLong(criterion, "DeletedBy"),
                    "基础信息→终端客户信息", "终端客户编号：" + MapUtils.getString(criterion, "DeletedEndUserNo"),
                    "删除");
        }

        return fetchForPage(criterion);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer batchChangeCatalogue(List<EndUserEntity> itemList) throws Exception {
        return _dao.batchChangeCatalogue(itemList);
    }
}
